import { createRouter, createWebHistory } from "vue-router";
import Index from "../views/index.vue";
import Register from "../views/Register.vue";
import Nofind from "../views/404.vue";
import Login from "../views/Login.vue";
import Home from "../views/Home.vue";
import InfoShow from "../views/InfoShow.vue";
import FoundList from "../views/FoundList.vue";
const routes = [
  {
    path: "/",
    redirect: "/index",
  },
  {
    path: "/login",
    name: "login",
    component: Login,
  },
  {
    path: "/index",
    component: Index,
    children: [
      { path: "", component: Home },
      { path: "/home", name: "home", component: Home },
      { path: "/infoshow", name: "infoshow", component: InfoShow },
      { path: "/foundlist", name: "foundlist", component: FoundList },
    ],
  },
  {
    path: "/register",
    name: "register",
    component: Register,
  },
  {
    path: "/:pathMatch(.*)",
    name: "/404",
    component: Nofind,
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});
//全局守卫：登录拦截 本地没有存token,请重新登录
router.beforeEach((to, from, next) => {
  const isLogin = localStorage.eleToken ? true : false;
  if (to.path == "/login" || to.path == "/register") {
    next();
  } else {
    isLogin ? next() : next("/login");
  }
});

export default router;
